package com.accp.demo.service.sale;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.accp.demo.mapper.sale.ISaleOutStorageMapper;
import com.accp.demo.pojo.PageBean;
import com.accp.demo.pojo.sale.SaleOutStorage;
import com.accp.demo.util.TestUtil;


@Service
public class SaleOutStorageService {

	@Autowired
	private ISaleOutStorageMapper iSaleOutStorageMapper;
	
	/**
	 * 新增主详记录  或修改
	 * @param map
	 * @return
	 */
	@Transactional
	public Boolean saveOrUpdate(Map<String, Object> map){
		try {
			if(map.get("createTime").toString().isEmpty()){
				//新增
				this.iSaleOutStorageMapper.addSaleOutStorage(map);	
				this.iSaleOutStorageMapper.addSaleOutStorageDetail(map);
			}else{
				//修改
				this.iSaleOutStorageMapper.updSaleOutStorage(map);
				this.iSaleOutStorageMapper.delSaleOutStorageDetail(map);
				this.iSaleOutStorageMapper.addSaleOutStorageDetail(map);
			}
		} catch (Exception e) {
			return false;
		}
		return true;
	}
	
	/**
	 * 查询所有主详记录
	 * @return
	 */
	public List<SaleOutStorage> findAll(){
		return this.iSaleOutStorageMapper.findAll();
	}
	
	/**
	 * 分页
	 * @param map
	 * @return
	 */
	public PageBean queryByPage(Map<String, Object> map){
		Integer currPage = Integer.valueOf(String.valueOf(map.get("page")));
		Integer pageSize = Integer.valueOf(String.valueOf(map.get("limit")));
		Integer count = this.iSaleOutStorageMapper.queryCount();
		List<SaleOutStorage> saleOutStorages = this.iSaleOutStorageMapper.queryByPage((currPage-1)*pageSize,pageSize);
		PageBean pageBean = new PageBean(pageSize, count, currPage, saleOutStorages);
		return pageBean;
	}
	
	/**
	 * 根据单据日期生成单据号码
	 * @param map
	 * @return
	 */
	public String getNewNo(Map<String, Object> map){
		return TestUtil.getNewNo(this.iSaleOutStorageMapper.getLastNo(map));
	}
	
	/**
	 * 修改主表的逻辑状态
	 * @param map	单据号码
	 * @return
	 */
	public Boolean updLogicalState(Map<String, Object> map){
		try {
			this.iSaleOutStorageMapper.updLogicalState(map);
		} catch (Exception e) {
			return false;
		}
		return true;
	}
	
	
	/**
	 * 修改销售出库单审核后等字段
	 * @param map
	 * @return
	 */
	public Boolean updSaleOutStorageExamineAfter(Map<String, Object> map){
		try {
			map.put("auditTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
			//修改主表审核后的字段
			this.iSaleOutStorageMapper.updSaleOutStorageExamineAfter(map);
			//修改销售订单的主表	条件: 转单
			
			//修改销售订单的从表 	条件:转单
			
			//修改仓库主表
			
			//新增仓库出入库记录
			
			//修改客户主表
			
			//新增客户账款表
		} catch (Exception e) {
			return false;
		}
		return true;
	}
	
}
